package com.crm.controller.reportsController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.OpportunityDAO;
import com.crm.dao.PendingOrdersDAO;
import com.crm.entity.Business;
import com.crm.entity.Opportunity;
import com.crm.entity.PO;

@Controller
@RequestMapping("/PendingOrd")
public class PendingOrderController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping("/show.do")
	public ModelAndView show(ModelAndView model,HttpSession  session,HttpServletRequest request,HttpServletResponse respone) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		//OpportunityDAO odao=new OpportunityDAO();
		PendingOrdersDAO pendOrdDAO=new PendingOrdersDAO();
		List<String> lipo=pendOrdDAO.getPendingOrders(busid);
		List<Opportunity> lopp=new ArrayList<Opportunity>();
		if (lipo!=null) {
			Iterator<String> itr = lipo.iterator();
			while (itr.hasNext() == true) {
				Object ob = itr.next();
				Integer p = (Integer) ob;
				Object obop = null;
				Opportunity op = null;
				try {
					obop = OpportunityDAO.getSingleInstance().getOppById(p, busid);
					op = (Opportunity) obop;
				} catch (Exception e) {
					e.printStackTrace();
				}
				PO pq = new PO();
				try {
					pq = pendOrdDAO.findById(p, busid);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (pq!=null) {
					Opportunity opp = new Opportunity();
					opp.setId(pq.getId());
					opp.setRefNum(op.getRefNum());
					opp.setRemarks(pq.getRemarks());
					lopp.add(opp);
				}
			}
		}
		System.out.println("List Of Opportunity Is: "+lopp);
		return model=new ModelAndView("/jsp/pending-orders.jsp")
		.addObject("submenu", "reports")
		.addObject("menuName","reports")
		.addObject("pendingOrders",lopp);	
	}

}
